<?php
namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Session;
use think\helper\Time;

use app\admin\model\Teacher;
use app\admin\model\Bus1;

class Busrecord1 extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'weiui';
    protected $model = null;
    protected $re_model = null;
    protected $weekarray = null;

    public function _initialize()
    {

        parent::_initialize();
        $this->model = new \app\admin\model\Busrecord;
        $this->teacher_model = new Teacher();
        $this->bus_model = new Bus1();
        $this->weekarray = array("日","一","二","三","四","五","六");

        $bus_list = $this->bus_model->order('times')->select();
        $tomorrow = date("Y/m/d",strtotime("+1 day"));

        $day = $this->weekarray[date("w",strtotime($tomorrow))];
        if($day=='日'||$day=='六')
        {
            unset($bus_list[2],$bus_list[5],$bus_list[6],$bus_list[10]);
        }
        $this->assign('tomorrow',$tomorrow);
        $this->assign('bus_list',$bus_list);

    }


    public function index()
    {
        if($this -> request -> isPost()){
            $params = $this->request->post("row/a");
            $where['type']= $params['type'];
            $where['tel']= $params['tel'];
            $where['nickname']= $params['nickname'];
            $is_teacher = $this->teacher_model->where($where)->find();

            switch ($params['type']) {
                case 1:
                    $typename = '在编人员';
                    break;
                default:
                    $typename = '编外人员';
                    break;
            }
            $data = ['type'=>$typename,'tel'=>$params['tel']];
            //不是老师或者编外人员
            if(!$is_teacher)
            {
                $this->error('该人员不存在','',['data'=>$data]);
            }

            $is_bus = $this->bus_model->where(['id'=>$params['time_slot']])->find();
            $params['times'] = $is_bus['times'];

            $is_bus2 = $this->bus_model->where(['id'=>$params['time_slot2']])->find();
            $params['times2'] = $is_bus2['times'];

            //当天预约最晚时间19：00
            $local_time =  date("H:i");
            if($local_time>='19:00')
            {
                $this->error('您好，预约已过期<br/>第二天乘车，<br/>需在前一天19点前预约','',['data'=>$data]);
            }
            if(strtotime($params['check_time'])<=strtotime(date("Y/m/d")))
            {
                $this->error('预约时间必须大于今天','',['data'=>$data]);
            }
            if(strtotime($params['check_time'])>='1631462400')
            {
                $this->error('您好，<br/>下周班车时间有调整<br/>暂不可预约','',['data'=>$data]);
            }
            $params['check_time'] = strtotime($params['check_time']);
            $where['check_time'] = $params['check_time'];
//            $where['state'] = $params['state'];
            $info = $this->model->where($where)->find();
            if(isset($info)){
                $row= $this->model->get($info['id']);
                $ret = $row->save($params);
            }else{
                $ret = $this->model->save($params);
            }
            $this->success('预约成功','',['nickname'=>$params['nickname'],'check_time'=>date('Y-m-d',$params['check_time']),'times'=>$params['times'],'address'=>$is_bus['address'],'times2'=>$params['times2'],'address2'=>$is_bus2['address'],'type'=>$params['type'],'tel'=>$params['tel']]);
        }
        $title = '平度班车';
        $this -> assign('pageTitle',$title);
        $this -> assign('wxcode','ques');
        return $this -> fetch();
    }

    public function check()
    {
        if($this -> request -> isPost()) {
            $params = $this->request->post("row/a");
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            $dispatchtimeBetween = [$beginToday,$endToday];
            $where['check_time'] = ['between',$dispatchtimeBetween];
            $where['tel'] = $params['tel'];
            $is_record = $this->model->where($where)->find();
            if(!empty($is_record)&&isset($is_record))
            {
                  // 先判断是去平度还是回青岛,根据当前时间,一个小时内
                  $start_minute = date('H:i',strtotime("-30 minutes"));
                  $end_minute = date('H:i',strtotime("+30 minutes"));
                  $minuteBetween = [$start_minute,$end_minute];
                  $where['times'] = ['between',$minuteBetween];
                  $record = $this->model->where($where)->find();

                  unset($where['times']);
                  $where['times2'] = ['between',$minuteBetween];
                  $record2 = $this->model->where($where)->find();

                  //去平度
                  if(!empty($record)&&isset($record)&&$record['is_bycar']==0)
                  {
                        //修改为已上车
                        $updata['is_bycar'] = 1;
                        $res = $this->model->save($updata,['id' => $record['id']]);
                        $this->success('上车成功','',['nickname'=>$is_record['nickname'],'check_time'=>$is_record['check_time_text'],'times'=>$record['times']]);
                  } else if(!empty($record2)&&isset($record2)&&$record['is_bycar2']==0){
                        //修改为已上车
                        $updata['is_bycar2'] = 1;
                        $res = $this->model->save($updata,['id' => $record2['id']]);
                        $this->success('上车成功','',['nickname'=>$is_record['nickname'],'check_time'=>$is_record['check_time_text'],'times'=>$record2['times2']]);
                  } else{
                      $this->error('上车时间已过期，请准时乘车');
                  }
            }else{ //没查到今天的预约
                $this->error('赞无今日预约记录');
            }
        }
        $title = '平度班车';
        $this -> assign('pageTitle',$title);
        return $this -> fetch();
    }
    public function view()
    {
//        $this->view->engine->layout(false);
        if($this -> request -> isPost()){
            $date = $this -> request -> post('create_time');
            $timestamp = strtotime($date);
            $timeArr = [$timestamp,$timestamp+(24*3600)-1];
            $this -> assign('date',$date);
        }else{
            $date = date("Y-m-d",strtotime("+1 day"));
            $timestamp = strtotime($date);
            $timeArr = [$timestamp,$timestamp+(24*3600)-1];
            //$timeArr = Time::today();
            $this -> assign('date',date("Y-m-d",strtotime("+1 day")));
        }

        $title = '平度班车';
        $this -> assign('pageTitle',$title);
        $busrecord_list = $this->bus_model->alias('b')
            ->join('bus_record r','b.times=r.times','left')
            -> whereBetween('r.check_time',$timeArr)
            ->group('b.times')
            ->order('b.id')
            ->field('b.*,r.id as r_id,r.check_time')
            ->select();
        if(!empty($busrecord_list)&&is_array($busrecord_list))
        {
            foreach ($busrecord_list as $k =>$value)
            {
                $where['check_time'] = $value['check_time'];
                $where['time_slot'] = $value['id'];
                $count = $this->model->where($where)->count();
                $busrecord_list[$k]['count'] = $count;
            }
        }
        $busrecord_list1 = $this->bus_model->alias('b')
            ->join('bus_record r','b.times=r.times2','left')
            -> whereBetween('r.check_time',$timeArr)
            ->group('b.times')
            ->order('b.id')
            ->field('b.*,r.id as r_id,r.check_time')
            ->select();
        if(!empty($busrecord_list1)&&is_array($busrecord_list1))
        {
            foreach ($busrecord_list1 as $k =>$value)
            {
                $where1['time_slot2'] =$value['id'];
                $where1['check_time'] = $value['check_time'];
                $count = $this->model->where($where1)->count();
                $busrecord_list1[$k]['count'] = $count;
            }
        }
        $this -> assign('busrecord_list',$busrecord_list);
        $this -> assign('busrecord_count',count($busrecord_list));

        $this -> assign('busrecord_list1',$busrecord_list1);
        $this -> assign('busrecord_count1',count($busrecord_list1));
        return $this -> fetch();
    }

    public function isbus()
    {
        $check_time = $this->request->post('check_time');
        //echo $check_time;die;
        $day = $this->weekarray[date("w", strtotime($check_time))];
        $bus_list = Db::name('bus1')->order('times')->select();
        //$bus_list = $this->bus_model->order('times')->select();
        if($day=='日'||$day=='六')
        {
            unset($bus_list[3],$bus_list[4],$bus_list[5],$bus_list[6]);
        }
        $this->success('', '', array_merge($bus_list, []));
    }

}